#include "spacewargame.h"
#include "gameobject.h"
#include <QKeyEvent>
#include <QtCore/qmath.h>

double get_distance(double dt, double v0, double a)
{
	static double _v0 = 0;
	static double _a = 0;
	static double _t = 0;

	if(dt == 0)
	{
		_t = 0;
		_v0 = v0;
		_a = a;
	}
	else
		_t += (dt/1000);
	
	return (_v0*_t + (_a*qPow(_t,2)/2)) / 1000;
}


SpaceWarGame::SpaceWarGame()
{
	m_ds = 0;
}

SpaceWarGame::~SpaceWarGame()
{	
}

QWidget* SpaceWarGame::mainWindow()
{	
	return &m_mainWindow;
}

void SpaceWarGame::Update(double elapsed)
{
}

bool SpaceWarGame::GameInitialize()
{
	SetGameWindow((GWindow*)m_mainWindow.gameWindow());

	m_pBg = new Background("bg", ":/Resources/Stars_large.png");
    GetWindow()->AddItem(m_pBg, 0, 0);
	//AddObject(m_pBg, 0, 0);
	
	m_screen = QSize(m_pBg->boundingRect().width(), m_pBg->boundingRect().height());
	m_world = m_screen * 3;
	
	m_pShip = new Ship("ship");
	AddObject(m_pShip, 0, 0);

	m_pMinimap = new Minimap("minimap", m_screen/4);
	GetWindow()->AddItem(m_pMinimap, m_screen.width()*3/4, m_screen.height()*3/4);
	//AddObject(m_pMinimap, m_screen.width()*3/4, m_screen.height()*3/4);
	
	return true;
}

static double init_pos_x = 0;

void SpaceWarGame::ProcessKeyEvent(QKeyEvent* event)
{
	//qDebug() << "ProcessKeyEvent";
	//double pos_x = 0;
	double distance = 0;
    if ( event->isAutoRepeat() )
	{
		distance = get_distance(m_elapT.elapsed(), 0, 0);		
		
		if(distance > m_world.width())// - m_pBg->boundingRect().width())
		{
			event->ignore();
			return;
		}
	}
	else
	{
		/*QPointF p = m_pShip->pos();
		QPointF s = m_pBg->scrollPos();
		qDebug() << "p" << p << "s" << s;
		init_pos_x = m_pShip->pos().x();*/

		m_ds = 0;
		m_elapT.restart();
		distance = get_distance(0, 3, 5);
	}

	//qDebug() << distance;
	double ds = distance - m_ds;
	
	switch(event->key())
    {
	case Qt::Key_Up:
		{
			double angle = m_pShip->rotation();
			double radian = degree_to_radian(180 - qAbs(angle));
			if(angle < 0)
				radian = -radian;
			double dx = m_pShip->pos().x()+qSin(radian)*ds;
			double dy = m_pShip->pos().y()+qCos(radian)*ds;

			
			/*if(m_world_x + dx > m_world.width() - m_screen.width()) 
				return;

			if(m_world_y + dy > m_world.height() - m_screen.height())
				return;*/

			if( (m_world_x + dx < m_world.width() - m_screen.width()) ||  
				(m_world_y + dy < m_world.height() - m_screen.height()) )
			{
				m_world_x += dx;
				m_world_y += dy;
				m_pBg->SetScroll(dx, dy);
			}
			else
				qDebug() << "err";
			
			QPointF p = m_pShip->pos();
			if( p.x() + dx < m_screen.width()/2 ||
				p.x() + dx > m_world.width() - m_screen.width()/2 )
			{
				m_pShip->setPos(dx, dy);
			}
			else
				qDebug() << "err";

			//	distance > m_world.width() - m_screen.width()/2)
			//{
			//	m_pShip->setPos(dx, dy);
			//	//qDebug() << "1disance" << distance << ds;
			//	//m_pShip->setScroll(distance);
			//}
			
			//m_pShip->SetScroll(dx, dy);
			
			
			//if(distance < m_screen.width()/2 ||
			//	distance > m_world.width() - m_screen.width()/2)
			//{
			//	m_pShip->setPos(dx, dy);
			//	//qDebug() << "1disance" << distance << ds;
			//	//m_pShip->setScroll(distance);
			//}

			/*if(distance < (m_world.width() - m_screen.width()) || 
				distance < (m_world.height() - m_screen.height()) )
				m_pBg->SetScroll(dx, dy);*/
			break;
		}
	case Qt::Key_Down:		
		{
			m_pShip->setPos(m_pShip->pos()-QPoint(distance,0));
			//scroll(distance, 0);
			break;
		}
    case Qt::Key_Left:		
		{
			m_pShip->setRotation(m_pShip->rotation()-distance);			
			break;
		}
    case Qt::Key_Right:     
		{
			m_pShip->setRotation(m_pShip->rotation()+distance);
			break;
		}
    default:
        event->ignore();
    }

	m_ds = distance;
}

QRect SpaceWarGame::GetScreenRectToWorld() const
{
	return QRect(QPoint(m_world_x, m_world_y), m_screen);
}

const QSize& SpaceWarGame::GetWorldSize()
{
	return m_world;
}